//
//  ViewController.m
//  HealthDemo
//
//  Created by SongGang on 2017/3/13.
//  Copyright © 2017年 NK. All rights reserved.
//

#import "ViewController.h"
#import "NKHealthKitManager.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *backGroupView;
@property (weak, nonatomic) IBOutlet UILabel *bushuLabel;
@property (weak, nonatomic) IBOutlet UILabel *lichengLabel;
@property (weak, nonatomic) IBOutlet UIButton *getNumberButton;
@property (weak, nonatomic) IBOutlet UILabel *loushuLabel;
@property (weak, nonatomic) IBOutlet UILabel *haibaLabel;
@property (weak, nonatomic) IBOutlet UILabel *seepLabel;
@property (weak, nonatomic) IBOutlet UILabel *fangxiangLabel;
@property (weak, nonatomic) IBOutlet UILabel *kaluliLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUPView];
}

- (void)setUPView {

    self.backGroupView.layer.cornerRadius = self.backGroupView.frame.size.width * 0.5;
    self.backGroupView.layer.borderWidth = 5;
    self.backGroupView.layer.borderColor = [UIColor colorWithDisplayP3Red:219 / 255.0 green:73 / 255.0 blue:92 / 255.0 alpha:1.0].CGColor;
    self.backGroupView.layer.masksToBounds = YES;
    
    self.bushuLabel.textColor = [UIColor colorWithDisplayP3Red:219 / 255.0 green:73 / 255.0 blue:92 / 255.0 alpha:1.0];
    self.lichengLabel.textColor = [UIColor colorWithDisplayP3Red:219 / 255.0 green:73 / 255.0 blue:92 / 255.0 alpha:1.0];
    self.loushuLabel.textColor = [UIColor colorWithDisplayP3Red:219 / 255.0 green:73 / 255.0 blue:92 / 255.0 alpha:1.0];
    
    self.getNumberButton.layer.cornerRadius = 5;
    self.getNumberButton.layer.masksToBounds = YES;
    self.getNumberButton.backgroundColor = [UIColor colorWithDisplayP3Red:219 / 255.0 green:73 / 255.0 blue:92 / 255.0 alpha:1.0];
}

- (IBAction)getBushuNumber:(UIButton *)sender {
    
    __weak typeof(self) weakSelf = self;
    NKHealthKitManager *manager = [NKHealthKitManager shareInstance];
    [manager authorizeHealthKit:^(BOOL success, NSError *error) {
        
        if (success) {
            
            // 步数
            [manager getSetpCount:^(double value, NSError *error) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    weakSelf.bushuLabel.attributedText = [self attributedTextWithString:[NSString stringWithFormat:@"%d步",(int)value] maxFont:75 minFont:20 backNumber:1 ];
                });
            }];
            
            // 里程
            [manager getDistance:^(double value, NSError *error) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    weakSelf.lichengLabel.attributedText = [self attributedTextWithString:[NSString stringWithFormat:@"%.1fkm",value] maxFont:40 minFont:20 backNumber:2];
                });
            }];
            
            // 楼层
            [manager getLightsClimbed:^(double value, NSError *error) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    weakSelf.loushuLabel.attributedText = [self attributedTextWithString:[NSString stringWithFormat:@"%.f层",value] maxFont:40 minFont:20 backNumber:1];
                });
            }];
            
            // 卡路里
            [manager getKilocalorieUnit:^(double value, NSError *error) {
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    weakSelf.kaluliLabel.attributedText = [self attributedTextWithString:[NSString stringWithFormat:@"%.fkcl",value] maxFont:40 minFont:20 backNumber:3];
                });
            }];
            
            // 获取定位信息(海拔，移动速度，方向)
            [manager getLocation:^(double altitude, double speed, NSString *direction) {
                 weakSelf.haibaLabel.attributedText = [self attributedTextWithString:[NSString stringWithFormat:@"%.fm",altitude] maxFont:40 minFont:20 backNumber:1];
                weakSelf.seepLabel.attributedText = [self attributedTextWithString:[NSString stringWithFormat:@"%.fm/s",speed] maxFont:40 minFont:20 backNumber:3];
            }];
        }
        
    }];
}

- (NSMutableAttributedString *)attributedTextWithString:(NSString *)string maxFont:(CGFloat)maxFont minFont:(CGFloat)minFont backNumber:(NSInteger)number{

    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:string];
    [att addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:minFont] range:NSMakeRange(string.length - number, number)];
    [att addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:maxFont] range:NSMakeRange(0, string.length - number)];
    return att;
}

@end
